const domainName = getApp().globalData.domainName
const oss_domain = getApp().globalData.oss_domain
const parameter = getApp().globalData.parameter


  // 删除该题
const delete_ti=(that,ti_id)=>{
    wx.showModal({
      content: '确定要删除该题吗？',
      showCancel: true,
      title: '提醒',
      success: res => {
        if(res.confirm){
          wx.request({
            url: domainName + '/delete_ti',
            data: {
              ti_id:ti_id,
            },...parameter,
            success: res => {
              if(res.data.update_res){
                wx.showToast({
                  title: '删除完成',
                  duration: 1500,
                  icon: 'success',
                })
                load_sec_list(that)
                load_ti_bank(that,sec_id)
              }
            },
          })
        }
      },
    })
  }



    // 确定设置
const confirm_set=(that)=>{
  getApp().get_vibration()
  wx.request({
    url: domainName + '/conf_sec_ti_submit',
    data: {
      ti_id:that.data.select_tiId,
      select_sec_id:that.data.select_sec_id,
    },...parameter,
    success: res => {
      if(res.data.row_id){
        wx.showToast({
          title: '设置完成！',
          duration: 1500,
          icon: 'success',
        })
        that.setData({
          list:res.data.list,
          select_tiId:null,
        })
        let sec_id = that.data.list[that.data.select_index].id
        load_ti_bank(that,sec_id)
      }
    },
  })
}

  

const load_sec_list=(that)=>{
    wx.request({
      url: domainName + '/load_secList_tiCount',
      data: {},...parameter,
      success: res => {
        that.setData({
          list:res.data.list,
        })
      },
    })
  }




  // 加载习题
const load_ti_bank=(that,section_id)=>{
    wx.request({
      url: domainName + '/tiKu_load_tiList_of_sec',
      data: {
        section_id:section_id,
      },...parameter,
      success: res => {
        that.setData({
          ti_list:res.data.list,
        })
        setTimeout(() => {
          that.setData({
            view_id:that.data.view_id,
          })
        }, 300);
      },
    })
  }


const tap_ti_opt=(that,e)=>{
  let index = e.currentTarget.id
  let itemList = ['归类','删除']
  let ti_id = that.data.ti_list[index].ti_id
  let sec_id = that.data.ti_list[index].sec_id
  wx.showActionSheet({
    itemList: itemList,
    itemColor: '#00b050',
    success: res => {
      let tapItem = itemList[res.tapIndex]
      if(tapItem=='归类'){
        that.setData({
          select_tiId:ti_id,
          select_tiId_secId_id:sec_id
        })
      }else if(tapItem=='删除'){
        delete_ti(that,ti_id, sec_id)
      }
    },
  })
}



  // 选择分组
const tap_section=(that,e)=>{
    let id = e.currentTarget.id
    let index = id.slice(4)
    let sec_id = that.data.list[index].id
    if(index == that.data.select_index){
      that.setData({
        select_index: null,
      })
    }else{
      that.setData({
        select_index:index,
        view_id:id
      })
      load_ti_bank(that,sec_id)
    }
  }

// pages/question_classify/question_classify.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    icon_url:oss_domain + 'static/picture/app_icon/',
    
    ti_list:[],
    select_tiId:null,
  },


  tap_ti_opt(e){tap_ti_opt(this, e)},
  tap_section(e){tap_section(this, e)},


  select_sec(e){
    this.setData({
      select_sec_id:e.detail.value
    })
  },

  // 取消设置
  cancel_set(){
    this.setData({
      select_tiId:null
    })
  },




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log('options',options)
    load_sec_list(this)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.setData({
      windowHeight:wx.getSystemInfoSync().windowHeight,
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      ti_list:[],
      list:[],
      select_tiId:null,
    })
    load_sec_list(this)
    wx.stopPullDownRefresh()
  },

})